package databasePackage;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

// Op dit moment alleen voor MySQL (geen Oracle ed)
public class DatabaseConnectionController {
	private String server, user, password, databaseName;
	public static final String JDBC_EXCEPTION = "JDBC Exception: ";
	public static final String SQL_EXCEPTION = "SQL Exception: ";
	public Connection connection;
	
	public DatabaseConnectionController(String server, String user, String password, String databaseName) {
		this.server = server;
		this.user = user;
		this.password = password;
		this.databaseName = databaseName;
	}

	public Connection openMySqlConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			String url = "jdbc:mysql://" + server + "/" + databaseName;

			/** Open connection */
			connection = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			System.err.println(JDBC_EXCEPTION + e);
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
		return connection;
	}

	public void closeConnection() {
		try {
			connection.close();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

	public void executeQuery(String query) {
		try {
			Statement statement = connection.createStatement();
			statement.executeQuery(query);
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
	}

	public ResultSet doQuery(String query) {
		ResultSet result = null;
		try {
			Statement statement = connection.createStatement();
			result = statement.executeQuery(query);
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
		return result;
	}
	
	public void insertQuery(String query) {
		try {
			Statement statement = connection.createStatement();
			statement.executeUpdate(query);
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
	}
	
	public void deleteQuery(String query) {
		try {
			Statement statement = connection.createStatement();
			String[] queries = query.split(";");
			for(String s : queries) {
				statement.addBatch(s);
			}
			statement.executeBatch();
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDatabaseName() {
		return databaseName;
	}

	public void setDatabaseName(String databaseName) {
		this.databaseName = databaseName;
	}
}
